package com.pingbu.tijian.hospital.backapi.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pingbu.common.bean.ResultBean;
import com.pingbu.common.utils.PbSecretUtils;
import com.pingbu.tijian.hospital.backapi.bean.entity.BuzhiZuoye;
import com.pingbu.tijian.hospital.backapi.bean.entity.UserInfo;
import com.pingbu.tijian.hospital.backapi.mapper.*;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BuzhiZuoyeService {

    private static final Logger logger = LoggerFactory.getLogger(BuzhiZuoyeService.class);

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private BuzhiZuoyeMapper buzhiZuoyeMapper;
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 布置作业的分页
     * @param param
     * @return
     */
    public ResultBean<PageInfo<BuzhiZuoye>> page(BuzhiZuoye param){

        PageHelper.startPage(param.getPageNum(),param.getPageSize());
        List<BuzhiZuoye> lst1 = buzhiZuoyeMapper.list(param);
        PageInfo<BuzhiZuoye> pi1 = new PageInfo<>(lst1);

        // 完善 ，遍历 lst1 里面的 create_user_id ，更新 createUserRealname
        for(BuzhiZuoye st: lst1){
            UserInfo ui1 = new UserInfo();
            ui1.setNoid(st.getCreate_user_id());
            UserInfo ui2 = userInfoMapper.get(ui1);
            if(ui2!=null){
                st.setCreateUserRealname(ui2.getRealname());
            }
        }

        return ResultBean.create(0,"success",pi1);
    }

    /**
     * 布置作业
     */
    public ResultBean<Integer> add(BuzhiZuoye param){

        //
        param.setNoid(PbSecretUtils.uuid());
        try {
            buzhiZuoyeMapper.insert(param);
        }catch (Exception err){
            return ResultBean.create(23,err.getMessage());
        }
        return ResultBean.create(0,"success");

    }

    /**
     * 删除作业
     * @param param
     * @return
     */
    public ResultBean<Integer> delete(BuzhiZuoye param){
        Integer res = buzhiZuoyeMapper.delete(param);
        return ResultBean.create(0,"success",res);
    }

    /**
     * 修改作业
     * @param param
     * @return
     */
    public ResultBean<Integer> update(BuzhiZuoye param){
        Integer res = buzhiZuoyeMapper.update(param);
        return ResultBean.create(0,"success",res);

    }


}
